package com.bilin.coin.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.bilin.coin.R;
import com.bilin.coin.adapter.MyPagerAdapter;
import com.bilin.coin.common.base.BaseFragment;
import com.bilin.coin.fragment.mill.MillResourceFragment;
import com.bilin.coin.fragment.mill.MyMillManageFragment;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

import butterknife.BindView;

public class MillFragment extends BaseFragment {
    @BindView(R.id.stSegmentTabLayout)
    SegmentTabLayout tl_2;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    protected ArrayList<BaseFragment> mFragmentList = new ArrayList<>();
    protected MyPagerAdapter pagerAdapter;
    private String[] mTitles_2 = {"我的矿机", "矿机资源"};

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        initViewPager();
    }

    private void initViewPager() {
        tl_2.setTabData(mTitles_2);
        tl_2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tl_2.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mFragmentList.add(new MyMillManageFragment());
        mFragmentList.add(new MillResourceFragment());
        pagerAdapter = new MyPagerAdapter(getChildFragmentManager(), mFragmentList, mTitles_2);
        viewPager.setAdapter(pagerAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mill;
    }

    public void onRefresh() {
        if (mFragmentList != null && mFragmentList.size() == 2) {
            MyMillManageFragment myMillManageFragment = (MyMillManageFragment) mFragmentList.get(0);
            MillResourceFragment millResourceFragment = (MillResourceFragment) mFragmentList.get(1);
            myMillManageFragment.onRefresh();
            millResourceFragment.onRefresh();
        }
    }
}
